/*
 * {CopyRightNotice}
 */
package jf.jextn.util;

import jf.jextn.JExtn;

/**
 * @author sandeep
 */
public class Text {

    /**
     * @param text
     * @return
     */
    public static String internalTrim(String text) {
        if (JExtn.hasValue(text)) {
            StringBuilder sb = new StringBuilder(text.length());
            boolean lastWhite = false;
            boolean ignore = false;
            for (int i = 0; i < text.length(); i++) {
                char ch = text.charAt(i);
                boolean isWhiteChar = (Character.isWhitespace(ch));
                if (isWhiteChar) {
                    if (!ignore && lastWhite) {
                        continue;
                    }
                    ch = ' '; // fix NL, LF, FF etc. chars
                    lastWhite = true;
                } else
                    lastWhite = false;

                ignore = ignore ^ (ch == '\'');

                sb.append(ch);
            }

            return sb.toString();
        }
        return "";
    }
}
